package com.example.tsbs.config;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    /**
     * 国家格式日期转换为yyyy-MM-dd
     * @param str
     * @return
     * @throws ParseException
     */
    public static String getTime(String str) throws ParseException {
//        String str = "Tue Jan 01 00:00:00 CST 1980";
        Date date = parse(str, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
//        System.out.printf("%tF %<tT%n", date);
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
    private static Date parse(String str, String pattern, Locale locale) {
        if(str == null || pattern == null) {
            return null;
        }
        try {
            return new SimpleDateFormat(pattern, locale).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将当前时间减去time分钟并返回
     */
    public static String timeProcess(Integer time) {
        Calendar rightNow = Calendar.getInstance();
        //rightNow.add(Calendar.MINUTE, -10); //减10分钟
        //rightNow.add(Calendar.HOUR,1);//加一个小时
        //rightNow.add(Calendar.DAY_OF_MONTH,1);//加一天
        //rightNow.add(Calendar.MONTH,1);//加一个月
        rightNow.add(Calendar.MINUTE,time);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去day天并返回
     */
    public static String dayProcess(Integer day) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DAY_OF_MONTH,day);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(dt1);
    }

    /**
     * 将当前时间减去hour小时并返回
     */
    public static String hourProcess(Integer hour) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.HOUR,hour);
        Date dt1 = rightNow.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(dt1);
    }

}
